import type {CEType, Viewer} from "vendor/components/pro-cesium";

function generatePosition(CE: CEType,num: number) {
    var list = []
    for (var i = 0; i < num; i++) {
        var lng = 120.38105869 + Math.random() * 0.5
        var lat = 31.10115627 + Math.random() * 0.5
        list.push(new CE.Position(lng, lat))
    }
    return list
}

/**
 * 矢量图层
 */
export const loadGlb = (CE: CEType, viewer: Viewer) => {
    let baselayer = CE.ImageryLayerFactory.createAmapImageryLayer()
    viewer.addBaseLayer(baselayer)
    let layer = new CE.VectorLayer('layer')
    viewer.addLayer(layer)
    let positions = generatePosition(CE,1)
    positions.forEach(item => {
        let model = new CE.Model(item, '/asserts/examples/model/GroundVehicle.glb')
        layer.addOverlay(model)

        // 添加click事件
        const Cesium = CE.Namespace.Cesium;
        model.on(Cesium.ScreenSpaceEventType.LEFT_CLICK, (e: any) => {
            console.log("点击事件e->", e)
        }, model)
    })
    viewer.flyTo(layer)
}


/**
 * Primitive 原始加载方法
 */
export const loadGlbByPrimitive = (CE: CEType, viewer: Viewer) => {
    function generatePosition(num:number) {
        let list = []
        for (let i = 0; i < num; i++) {
            let lng = 120.38105869 + Math.random() * 0.5
            let lat = 31.10115627 + Math.random() * 0.5
            list.push(new CE.Position(lng, lat,1000 * Math.random(),3600 * Math.random()))
        }
        return list
    }

    let baselayer = CE.ImageryLayerFactory.createAmapImageryLayer()
    viewer.addBaseLayer(baselayer)
    let layer = new CE.PrimitiveLayer('layer')
    viewer.addLayer(layer)
    let positions = generatePosition(100)
    positions.forEach(item => {
        let model = new CE.ModelPrimitive(item, '/asserts/examples/model/Cesium_Air.glb')
        model.setStyle({scale:50})
        layer.addOverlay(model)

        // 添加click事件
        const Cesium = CE.Namespace.Cesium;
        model.on(Cesium.ScreenSpaceEventType.LEFT_CLICK, (e: any) => {
            console.log("点击事件e->", e)
        }, model)
    })

    viewer.flyToPosition("120.82005120445152,30.903795335982288,60975.10826917929,341.02,-50.29")
}